package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private IUserService userService;

    private final StringRedisTemplate stringRedisTemplate;

    public FollowServiceImpl(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    /**
     * 判断是否关注当前博主
     * @author daoketa
     * @param
     *
     */
    @Override
    public boolean isFollow(Long followUserId) {
        // 获取当前用户 id
        Long userId = UserHolder.getUser().getId();
        // 查询是否关注当前博主
        Integer count = this.query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        return count > 0;
    }

    /** 关注或取关博主 */
    @Override
    public void follow(Long followUserId, boolean isFollow) {
        // 获取当前用户 id
        Long userId = UserHolder.getUser().getId();
        // set 集合 key
        String key = "follows:"+userId;
        if (isFollow){
            // 关注
            // 添加到数据库的关注表
            Follow follow = new Follow();
            follow.setFollowUserId(followUserId);
            follow.setUserId(userId);
            boolean flag = this.save(follow);
            // 添加到用户的关注 set 集合, sadd userId followerUserId
            if (flag){
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
            }
        }else {
            // 取关
            // 从数据库的关注表移除
            boolean flag = this.remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId).eq("follow_user_id", followUserId));
            // 删除用户 set 集合中的博主 id
            if (flag){
                stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
            }
        }
    }

    /** 获取共同关注的用户列表 */
    @Override
    public List<UserDTO> followCommon(Long followUserId) {
        // 获取当前用户 id
        Long userId = UserHolder.getUser().getId();
        // 获取 set 集合 key
        String key1 = "follows:"+userId;
        String key2 = "follows:"+followUserId;
        // 取交集
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if (intersect == null || intersect.isEmpty()){
            // 无交集
            return Collections.emptyList();
        }
        // 解析 id 集合
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        // 查询用户列表
        List<User> users = userService.listByIds(ids);
        List<UserDTO> collect = users.stream()
                                .map(one -> BeanUtil.copyProperties(one, UserDTO.class))
                                .collect(Collectors.toList());
        return collect;
    }


}
